import { getCityLabel } from '../../api/checkFlight/checkFlight'
import pyjs from '../../miniprogram_npm/js-pinyin/index'

Page({
  data: {
    cityList: [],
  },
  async getCityLabel() {
    let { result } = await getCityLabel()
    if (result) {
      let datas = {}
      result.splice(0, 1)
      let cfKey = {}
      result.map((item) => {
        if (item.CITY_EN == 'MACAU') {
          item.CITY_EN = 'MACAO'
        }
        let pyszm = item.CITY_EN.substr(0, 1)
        if (this.data.lang != 1) {
          pyszm = item.CITY_EN.substr(0, 1)
        } else {
          pyszm = pyjs.getFullChars(item.CITY_CH).slice(0, 1)
        }
        var carr = datas[pyszm] || []
        let temp = {
          name: this.data.lang == 3 ? item.CITY_EN : item.CITY_CH,
          abridgeName: item.CITY_EN_ABBR,
        }
        if (!cfKey[temp.name]) {
          carr.push(temp)
          cfKey[temp.name] = temp.abridgeName
        }
        datas[pyszm] = carr
      })
      // console.log(datas)
      let cDatas = []
      let idxList = Object.keys(datas).sort()
      for (let i in idxList) {
        let szm = idxList[i]
        cDatas.push({
          idx: szm,
          list: datas[szm],
        })
      }
      this.setData({
        indexList: idxList,
        globalCityList: cDatas,
        cityList: cDatas,
      })
      // console.log(this.data.indexList)
      // console.log(cDatas)
    }
  },
  selectCity(e) {
    let item = e.target.dataset.item
    // console.log(item)
    var pages = getCurrentPages()
    var prevPage = pages[pages.length - 2] //上一个页面
    let data = {}
    // debugger
    if (this.data.selItem == 1) {
      data.dCityTxt = item.name
      data.abridgeDCityTxt = item.abridgeName
    } else {
      data.aCityTxt = item.name
      data.abridgeACityTxt = item.abridgeName
    }
    prevPage.setData(data)
    wx.navigateBack({
      delta: 1,
    })
  },
  onLoad(opt) {
    this.setData({
      lang: wx.getStorageSync('lang'),
      selItem: opt.selItem,
    })
    wx.setNavigationBarTitle({
      title: opt.title,
    })
    this.getCityLabel()
  },
})
